//
//  File.swift
//  elarc01
//
//  Created by elarc on 16/8/16.
//  Copyright © 2016年 elarc. All rights reserved.
//

import Foundation

class ElarcPickerViewController: UIViewController {
    
    let label = UILabel()
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = UIColor.whiteColor()
            // Do any additional setup after loading the view, typically from a nib.
            treeTest()
            buildNumLabel()
            
            
        }
    
    func buildNumLabel() -> Void {
        label.frame = CGRectMake(100, 100, 100, 100)
        label.center = self.view.center
        label.textColor = UIColor.randomColor()
        self.view.addSubview(label)
       
//        let LabelAnimation2 =  NumberAnimation(label: label2, TotheValue: "2819")
//        let LabelAnimation3 =  NumberAnimation(label: label3, TotheValue: "56")
    }
    
    
        func treeTest() -> Void {
            
            let items = ["AlonePicker", "DatePicker", "AreaPicker","chooseItem"]
            let segmentedControl = UISegmentedControl(items: items)
            segmentedControl.selectedSegmentIndex = 0
            segmentedControl.layer.cornerRadius = 5.0
            segmentedControl.backgroundColor = UIColor.whiteColor()
            segmentedControl.tintColor = UIColor(white: 0.2, alpha: 1)
            segmentedControl.frame = CGRectMake(100, 100, 300, 40)
            segmentedControl.frame = CGRectMake(50, 100, self.view.frame.width-100, 44)
            segmentedControl.addTarget(self, action: #selector(segmentedControlTouch), forControlEvents: .ValueChanged)
            self.view.addSubview(segmentedControl)
            
        }
        
        func segmentedControlTouch(sender: UISegmentedControl) -> Void{
            
            switch sender.selectedSegmentIndex {
            case 0:
                showAlonePicker()
                _ = NumberAnimation(label: label, TotheValue: "119874")
            case 1:
                showDatePicker()
                _ =  NumberAnimation(label: label, TotheValue: "2819")
            case 2:
                showAreaPicker()
                _ =  NumberAnimation(label: label, TotheValue: "56")
            case 3:
                showChooseItemPicker()
            default:
                showAreaPicker()
            }
            
        }
    
        // MARK: - AlonePicker
    // MARK: - ItemChoose
    func showChooseItemPicker() -> Void {
        var dataSource = [String]()
        let title = "chooseItem"
        let limitCount = 3
        dataSource = [
                "网剧",
                "电视剧",
                "舞台剧",
                "音乐剧",
                "广播剧",
                "院线电影",
                "综艺节目",
                "网络大电影"
            ]

        let chooseItemAlert = ElarcPickerView(aDataSource: dataSource,limitCount:limitCount, aTitle: title)
        chooseItemAlert.show()
        chooseItemAlert.didClickDoneForTypeChooseItemHandler { (result) in
            
            if result.count != 0 {
            let resultStr = result.joinWithSeparator("、")
              print(resultStr)
            }
        }
        chooseItemAlert.didClickCancelHandler {
            
        }
    }
        // MARK: - AlonePicker
        private let alonePicker: ElarcPickerView = {
            var dataSource = [String]()
            for i in 0..<20 {
                dataSource.append("\(i)")
            }
            return ElarcPickerView(aDataSource: dataSource, aTitle: "AlonePicker")
        }()
        func showAlonePicker() -> Void {
            alonePicker.show()
            alonePicker.showSelectedRow(3, animated: true)
            alonePicker.didClickDoneForTypeAloneHandler { (selectedRow, result) in
                print("selectedRow:\(selectedRow)")
                print("result:\(result)")
            }
            alonePicker.didClickCancelHandler {
                print("dismiss")
            }
        }
        
        // MARK: - DateModePicker
        private let datePicker: ElarcPickerView = {
            return ElarcPickerView(aDatePickerMode: .Date, aTitle: "DatePicker")
        }()
        
        func showDatePicker() -> Void {
            datePicker.show()
            datePicker.setDate(NSDate(), animated: true)
            datePicker.didClickDoneForTypeDateWithFormat("yy年MM月dd日 HH:mm:ss") { (selectedDate, dateString) in
                print("selectedDate:\(selectedDate)")
                print("dateString:\(dateString)")
            }
            datePicker.didClickCancelHandler {
                print("dismiss")
            }
        }
        
        // MARK: - AreaPicker
        
        private let areaPicker: ElarcPickerView = {
            return ElarcPickerView(anAreaType: .ProvinceCityDistrict, aTitle: "AreaPicker")
        }()
        
        func showAreaPicker() -> Void {
            areaPicker.show()
            areaPicker.didClickDoneForTypeAreaHandler { (province, city, district) in
                print("province:\(province)")
                print("city:\(city)")
                print("district:\(district)")
            }
            areaPicker.didClickCancelHandler {
                print("dismiss")
            }
        }
        
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        
        
}

